package com.benbi.shequapi.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.benbi.shequapi.bean.*;
import com.benbi.shequapi.mapper.*;
import com.benbi.shequapi.service.RecordService;
import com.benbi.shequapi.service.ReportService;
import com.benbi.shequapi.utils.Result;
import com.benbi.shequapi.utils.ResultData;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;

@Service
public class RecordImpl implements RecordService {
    @Resource
    RecordMapper recordMapper;
    @Resource
    PersonMapper personMapper;

    @Override
    public Result addRecord(int r_pid,int recorder_id,int form,float temp,String formAddr, String business) {
        Date d = new Date();
        Record r = new Record(r_pid,temp,d,form,recorder_id,formAddr,business);
        int p = recordMapper.addRecord(r);
        return new Result(0, "成功");

    }

    @Override
    public Result getRecordStatis(Date d) {
        List<Record> getList;
        getList = recordMapper.getRecordStatis(d);
        int dayAccess = 0;
        int daySignin = 0;
        int dayWarn = 0;
        for (int i = 0; i < getList.size(); i++) {
            if(getList.get(i).getForm() == 1){
                dayAccess++;
            }
            if(getList.get(i).getTemp()>37.3) {
                dayWarn++;
            }
        }
        daySignin = getList.size()-dayAccess;

        JSONObject o = new JSONObject();
        o.put("daySignin", daySignin);
        o.put("dayAccess", dayAccess);
        o.put("dayTotal", getList.size());
        o.put("dayWarn", dayWarn);


        if (o == null) {return new Result(1, "不存在");}
        return new ResultData(o);
    }

    @Override
    public Result getRecordSignin(Date d) {
        List<Record> p = recordMapper.getRecordSignin(d);
        List<JSONObject> lo = new LinkedList<>();
        for (Record record : p) {
            JSONObject o = new JSONObject();
            o.putAll((JSONObject)JSONObject.toJSON(record));
            o.put("name", personMapper.selectPersonNameById(record.getR_pid()));
            lo.add(o);
        }
        return new ResultData(lo);
    }

    @Override
    public Result getRecordAccess(Date d) {
        List<Record> p = recordMapper.getRecordAccess(d);
        List<JSONObject> lo = new LinkedList<>();
        for (Record record : p) {
            JSONObject o = new JSONObject();
            o.putAll((JSONObject)JSONObject.toJSON(record));
            o.put("name", personMapper.selectPersonNameById(record.getR_pid()));
            lo.add(o);
        }
        return new ResultData(lo);
    }

    @Override
    public Result getRecordTotal(Date d) {
        List<Record> getList = recordMapper.getRecordTotal(d);
        List<JSONObject> lo = new LinkedList<>();
        for (Record record : getList) {
            JSONObject o = new JSONObject();
            o.putAll((JSONObject)JSONObject.toJSON(record));
            o.put("name", personMapper.selectPersonNameById(record.getR_pid()));
            lo.add(o);
        }
        return new ResultData(lo);
    }

    @Override
    public Result getRecordWarn(Date d) {
        List<Record> getList = recordMapper.getRecordWarn(d);
        List<JSONObject> lo = new LinkedList<>();
        for (Record record : getList) {
            JSONObject o = new JSONObject();
            o.putAll((JSONObject)JSONObject.toJSON(record));
            o.put("name", personMapper.selectPersonNameById(record.getR_pid()));
            lo.add(o);
        }
        return new ResultData(lo);
    }


}
